using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MMM.Core.Domain;
using ServiceStack.ServiceHost;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using MMM.Services.Base;
using MMM.Services.Operations;
using MMM.Services.Type;
using MMM.Core;
using MMM.Services.Response;
using ServiceStack.ServiceInterface;

namespace MMM.Services
{
    [Route("/api/bagmanageinfo", "PUT")]
    public class BagManageInfoList : List<BagManageInfo>
    {
    }
    public class BagManageInfoListService : RestServiceBase<BagManageInfoList>
    {
        public override object OnPut(BagManageInfoList request)
        {
            IRepository<BagManageInfo> bmRep = ApplicationContext.Container.GetInstance<IRepository<BagManageInfo>>();
            if (request != null)
            {
                foreach (var item in request)
                {
                    if (item.IsAdd)
                    {
                        item.IsAdd = false;
                        bmRep.Add(item);
                    }
                    else
                    {
                        bmRep.Update(item);
                    }
                }
            }
            return OperationResult<BagManageInfo>.SuccessRespose;
        }
    }
    /// <summary>
    /// 类描述 基础服务类(POST,PUT)
    /// </summary>
    public class BagManageInfoService : BaseService<BagManageInfo>
    {
    }
    /// <summary>
    /// 类描述 服务类(GET)
    /// </summary>
    public class BagManageInfoGetSerice : BaseGetService<BagManageInfoPageGet, BagManageInfo>
    {
        //protected override System.Linq.Expressions.Expression<Func<BagManageInfo, bool>> CreateFilter(BagManageInfoPageGet request)
        //{
        //    return base.CreateFilter(request);
        //}
        protected override string CreateFilter(BagManageInfoPageGet request)
        {
            string sql = string.Empty;
            IList<Filter> lst = request.GetFilter();
            if (lst != null)
            {
                foreach (var item in lst)
                {
                    if (item.Property == "ContractCode" || item.Property == "AreaCode")
                    {
                        if (string.IsNullOrWhiteSpace(sql))
                        {
                            sql = item.ToSqlEque();
                        }
                        else
                        {
                            sql = string.Format(" {0} and {1}", sql, item.ToSqlEque());
                        }
                    }
                    else if (item.Property == "CompanyID")
                    {
                        item.Value = Guid.Parse(item.Value).ToString();
                        if (string.IsNullOrWhiteSpace(sql))
                        {
                            sql = item.ToSqlEque();
                        }
                        else
                        {
                            sql = string.Format(" {0} and {1} ", sql, item.ToSqlEque());
                        }
                    }
                    else
                    {
                        if (string.IsNullOrWhiteSpace(sql))
                        {
                            sql = item.ToSqlLike();
                        }
                        else
                        {
                            sql = string.Format(" {0} and {1} ", sql, item.ToSqlLike());
                        }
                    }
                }
            }
            return sql;
        }
        public override object OnGet(BagManageInfoPageGet request)
        {
            IRepository<ProductInfo> pdRep = ApplicationContext.Container.GetInstance<IRepository<ProductInfo>>();
            IRepository<BagManageInfo> bmRep = ApplicationContext.Container.GetInstance<IRepository<BagManageInfo>>();
            if (request.isAdd)
            {
                OperationResult<ProductInfo> pIList = new OperationResult<ProductInfo>(pdRep.GetEntities(request.Page, request.Limit, CreateFilter(request)));
                var pdList = pIList.Data;
                List<BagManageInfo> bmList = new List<BagManageInfo>();

                var bmList1 = bmRep.GetAll(t => t.ContractCode == request.ContractCode);
                if (pdList != null)
                {
                    foreach (var item in pdList)
                    {
                        BagManageInfo model = bmList1.FirstOrDefault(t => t.ProductID == item.Id);
                        BagManageInfo bagModel = new BagManageInfo();
                        if (model != null)
                        {
                            bagModel = model;
                        }
                        else
                        {
                            bagModel.Id = Guid.NewGuid();
                            bagModel.AreaCode = item.AreaCode;
                            bagModel.AreaName = item.AreaName;
                            bagModel.Product = item.ProductName;
                            bagModel.ProductID = item.Id;
                            bagModel.CompanyID = request.CompanyID;
                            bagModel.ContractCode = request.ContractCode;
                            bagModel.ProductType = item.ProductType;
                            bagModel.Price = 0;
                            bagModel.IsAdd = true;
                        }

                        bmList.Add(bagModel);
                    }
                }
                return new OperationResult<BagManageInfo>(bmList, pIList.TotalCount);
            }
            else
            {
                return base.OnGet(request);
            }
        }
    }
    /// <summary>
    /// 类描述 服务类(DELETE)
    /// </summary>
    public class BagManageInfoDeleteService : BaseDeleteService<BagManageInfo, BagManageInfoDelete>
    {
    }
}
